from panda3d.core import Vec2, Point3, Vec3, Vec4
from menu_button import MenuButton
import copy
from dialog import Dialog
from object_const import ObjectConstants
from types import StringType
'''

'''
class LevelEditorHud:	
	def __init__(self, parent, levelEditor):
		self.parent = parent
		self.levelEditor = levelEditor
		self.const = ObjectConstants()
		self.selection = self.const.SAND
		self.background = self.parent.loader.loadModelCopy("models/plane")
		self.backgroundTexture = self.parent.loader.loadTexture("models/menu/hud/level_editor_hud_background.png")
		self.background.setTexture(self.backgroundTexture)
		self.background.setPos(0, 0, -0.70)
		self.background.setScale(2.67, 1, 0.6)
		self.background.reparentTo(self.parent.base.aspect2d)
		
		self.models = dict()
		
		self.models[self.const.SAND] = self.loadModel(self.const.SAND, Vec3(-0.5,0,-0.65), text="Ground Textures")
		self.models[self.const.GRASS] = self.loadModel(self.const.GRASS, Vec3(-0.3,0,-0.65))
		self.models[self.const.DIRTY_ROCK] = self.loadModel(self.const.DIRTY_ROCK, Vec3(-0.1,0,-0.65))
		self.models[self.const.ROCK] = self.loadModel(self.const.ROCK, Vec3(0.1,0,-0.65))
		self.models[self.const.WATER] = self.loadModel(self.const.WATER, Vec3(0.3,0,-0.65))
		
		self.models[self.const.CRACKED_SAND] = self.loadModel(self.const.CRACKED_SAND, Vec3(-0.5,0,-0.85))
		self.models[self.const.CRACKED_ROCK] = self.loadModel(self.const.CRACKED_ROCK, Vec3(-0.3,0,-0.85))
		self.models[self.const.DIRT] = self.loadModel(self.const.DIRT, Vec3(-0.1,0,-0.85))
		self.models[self.const.CRACKED_DIRT] = self.loadModel(self.const.CRACKED_DIRT, Vec3(0.1,0,-0.85))
		
		self.models['save'] = self.loadModel('save', Vec3(-1.0,0,-0.55), scale=Vec3(0.45, 1, 0.15), texture="models/menu/hud/editor_save")
		self.models['load'] = self.loadModel('load', Vec3(-1.0,0,-0.70), scale=Vec3(0.45, 1, 0.15), texture="models/menu/hud/editor_load")
		self.models['exit'] = self.loadModel('exit', Vec3(-1.0,0,-0.85), scale=Vec3(0.45, 1, 0.15), texture="models/menu/hud/editor_exit")
		
		self.models['save_dialog'] = Dialog(self.parent, 'save', headerText="Save Level")
		self.models['save_dialog'].hide()
		self.models['save_dialog'].addInput(2, 'filename')
		self.models['save_dialog'].addInput(4, 'level_name', initialText="Enter level name")
		self.models['save_dialog'].addOkCancel(self.save)
		
		self.models['load_dialog'] = Dialog(self.parent, 'load', headerText="Load Level")
		self.models['load_dialog'].hide()
		self.models['load_dialog'].addInput(3, 'filename')
		self.models['load_dialog'].addOkCancel(self.load)
		
		self.models[self.const.MINERALS] = self.loadModel(self.const.MINERALS, Vec3(0.95,0,-0.65), text="Resources", scale=0.1)
		self.models[self.const.VESPENE] = self.loadModel(self.const.VESPENE, Vec3(0.95,0,-0.75), scale=Vec3(0.1, 0.1, 0.075))
		self.models[self.const.COMMAND_CENTER] = self.loadModel(self.const.COMMAND_CENTER, Vec3(0.95,0,-0.85), scale=0.1)
		self.models[self.const.INFESTED_COMMAND_CENTER] = self.loadModel(self.const.INFESTED_COMMAND_CENTER, Vec3(0.75,0,-0.65), scale=0.1)
		self.models[self.const.ZERGLING] = self.loadModel(self.const.ZERGLING, Vec3(0.75,0,-0.75), scale=0.1)
		
		self.startMouseWatch()
	
	'''
		Gets the type of object that is selected
	'''
	def getSelection(self):
		objectType = self.const.TEXTURE
		if (self.const.isObject(self.selection)):
			objectType = self.const.OBJECT
		return (objectType, self.models[self.selection][1])
	
	'''
		Loads the correct item as a model and as a texture
	'''
	def loadModel(self, switch, pos, text=None, scale=0.2, texture=""):
		if (texture != ""):
			name = switch + "_texture"
		else:
			(texture, name) = self.const.getTextureFromNo(switch, extension=False)			
		
		model = MenuButton(self.parent, name, texture, self.setSelection, pos, isMovie=False,
			scale=scale, text=text, textPos=Vec3(-0.10,0,0.15), textScale=Vec3(0.05,0.05,0.0375), 
			extraArgs=[switch])
		return (model, model.model.getTexture())
	
	'''
		Sets the selection to the correct button press
	'''
	def setSelection(self, switch):
		self.selection = switch
		if (self.selection not in ('save','exit','load')):
			texture = self.getSelection()
			if (texture[0] == self.const.TEXTURE and self.parent.levelEditor.selected):
				self.parent.levelEditor.selected.setTexture(texture[1])
		else:
			self.handleHudCommand()
			
	'''
	
	'''
	def handleHudCommand(self):
		if (self.selection == 'save'):
			self.models['save_dialog'].show()
		elif (self.selection == 'load'):
			self.models['load_dialog'].show()
		elif (self.selection == 'exit'):
			self.levelEditor.exit()
	
	def save(self):
		self.levelEditor.save(self.models['save_dialog'].getText('filename'), 
			self.models['save_dialog'].getText('level_name'))
		self.models['save_dialog'].hide()
	
	def load(self):
		self.levelEditor.load(self.models['load_dialog'].getText('filename'))
		self.models['load_dialog'].hide()
	
	def startMouseWatch(self):
		self.parent.cameraController.startMouseWatch()
		
	def endMouseWatch(self):
		self.parent.cameraController.endMouseWatch()
	
	def destroy(self):
		for key in self.models.keys():
			if (isinstance(key, StringType) and len(key.split('_')) > 1):
				self.models[key].removeNode()
			else:
				self.models[key][0].removeNode()
			
		self.background.removeNode()
		self.endMouseWatch()
		
		